<template>
	<view class="content">

		<view class="bangfu">

			<view class="kuang1">
				<view v-for="(item, index) in cate">
					<view class="kk" v-if="item.id === 1" @tap="to_apply(item.id,item.name)">
						<view>会员慰问</view>
						<!-- @/static/wugong/label. -->
						<image src="@/static/activity/apply/new/hyww.png" mode=""></image>
					</view>
				</view>
			</view>
			<view class="kuang1">
				<view v-for="(item, index) in cate">
					<view class="kk" v-if="item.id === 2" @tap="to_apply(item.id,item.name)">
						<view>医疗互助</view>
						<image src="@/static/activity/apply/new/ylhz.png" mode=""></image>
					</view>
				</view>
			</view>

			<view class="kuang" @tap="tiao">
				<view>困难帮扶</view>
				<image src="@/static/activity/apply/new/knbf.png" mode=""></image>
			</view>
			<view class="kuang" @tap="tiao">
				<view>心灵驿站</view>
				<image src="@/static/activity/apply/new/xlyz.png" mode=""></image>
			</view>
			<view class="kuang" @tap="tiao">
				<view>重疾救护</view>
				<image src="@/static/activity/apply/new/zjjh.png" mode=""></image>
			</view>
			<view class="kuang" @tap="tiao">
				<view>金秋助学</view>
				<image src="@/static/activity/apply/new/jqzx.png" mode=""></image>
			</view>

		</view>

		<!-- 公示公告 -->
		<view class="gsgg" @tap="to_gs">
			<text class="gui-h3">公示公告</text>
		</view>

		<menuBar menu_index="0" />

	</view>
</template>
<script>
	import menuBar from './menu_bar.vue';
	export default {
		components: {
			menuBar
		},
		data() {
			return {
				user: [],
				cate: [],
				shuju: [],
				last: [],
				touchNum: 0,
			}
		},
		onReady() {
			this.user = uni.getStorageSync("member");
			this.get_hzj_cate();
		},
		onLoad(opt) {
			uni.showLoading({
				title: '登录中...'
			});
			if (opt.Bearer) {
				uni.setStorageSync("Bearer", opt.Bearer);
				this.bearer();
			} else if (opt.code) {
				this.loginx(code);
			} else {
				this.user = uni.getStorageSync("member");
			}
		},
		methods: {
			bearer() {
				let self = this
				this.$boya.NGet("/app/auth/user", null, function(res) {
					console.log("bearer: ", res);
					uni.hideLoading();
					uni.setStorageSync("member", res.data.member);
					self.user = res.data.member;
					uni.showLoading({
						title: '加载中...'
					});
					self.get_hzj_cate();
				}, err => {
					console.log('bearerErr: ', err);
					uni.showToast({
						title: '登录失败,请重试',
						duration: 2000,
						icon: 'error'
					});
				}, false, false)
			},
			loginx(code) {
				let self = this
				console.log('code: ', code);
				self.$boya.Request({
					url: '/public/loginx',
					data: {
						code: code,
					}
				}, function(res) {
					console.log("loginx", res);
					if (res.code == 200) {
						self.user = res.data
						uni.setStorageSync("member", res.data);
						uni.showLoading({
							title: '加载中...'
						});
						self.get_hzj_cate();
					} else {
						uni.showToast({
							title: '登录失败,请重试',
							duration: 2000,
							icon: 'error'
						});
					}
				}, false)
			},
			to_apply(id, name) {
				console.log('id: ', id);
				// console.log('name: ', name);
				// console.log(this.user, 'user');
				let self = this;
				if (id == 1) {
					uni.navigateTo({
						url: '/pages/activities/apply/index/type?category=' + id + '&cate_name=' + name
					})
				} else if (id == 2) {

					let url = '/hzj_apply/get_apply'
					let parm = {
						pages: 0,
						user_id: self.user.id,
						apply_id: 0,
						switch_index: 0
					}
					this.$boya.NPost(url, parm, function(res) {
						let data = res.data
						self.shuju = []
						for (var i = 0; i < data.length; i++) {
							if (id == data[i].cate_id) {
								if (data[i].status == 10) {
									self.shuju.push(data[i])
								}
							}
						}
						if (self.shuju.length != 0) {
							// 获取当天日期后，获取当天时间存在的年份
							let now = new Date().getFullYear();
							self.last = []
							for (var j = 0; j < self.shuju.length; j++) {
								// 获取当前类型所有数据的创建时间的时间戳
								let created_at = Date.parse(self.shuju[j].created_at);
								// 时间格式化
								let shijianchuo = new Date(parseInt(created_at));
								// 获取数据创建日期后，获取数据创建日期的年份
								let shijian = shijianchuo.getFullYear()
								// console.log(shijian);
								self.last.push(shijian)

								let liangci = []
								for (var k = 0; k < self.last.length; k++) {
									if (self.last[k] == now) {
										liangci.push(self.last[k])
									}
								}

								if (liangci.length >= 2) {
									uni.showModal({
										title: '该类型一年只可申报两次',
										content: '您最少有两条申请在受理中或已通过',
										success(res) {
											uni.navigateTo({
												url: './my_apply'
											});
										}
									})
								} else {
									uni.showModal({
										title: '提示',
										content: '是否报备申请',
										success(res) {
											if (res.confirm) {
												uni.navigateTo({
													url: './apply?category=' + id +
														'&cate_name=' +
														name
												});
											} else {
												uni.navigateTo({
													url: './my_apply'
												});
											}
										}
									})
								}
							}
						} else {
							uni.showModal({
								title: '提示',
								content: '是否报备申请',
								success(res) {
									if (res.confirm) {
										uni.navigateTo({
											url: './apply?category=' + id + '&cate_name=' +
												name
										});
									} else {
										uni.navigateTo({
											url: './my_apply'
										});
									}
								}
							})
						}
					})
				}
			},
			to_zc() { //查看政策
				uni.navigateTo({
					url: './policy'
				});
			},
			to_gs() { //查看公示
				uni.navigateTo({
					url: '/pages/activities/apply/index/publicity'
				});
			},
			get_hzj_cate() {
				let self = this;
				let url = '/hzj_apply/get_category'
				let parm = {
					c_id: 0
				}
				self.$boya.NPost(url, parm, function(res) {
					console.log('shuju: ', res);
					if (res.code == 200) {
						self.cate = res.data;
					} else {
						self.tip('0', res.data);
					}
					uni.hideLoading();
				})
			},
			to_my_apply() {
				let self = this;
				if (this.user) {
					uni.navigateTo({
						url: './my_apply'
					});
				} else {
					uni.showModal({
						title: '提示信息',
						content: "未登录!!",
						cancelText: '确定',
						confirmText: '取消',
						success: function(res) {
							if (res.confirm) {} else if (res.cancel) {}
							uni.reLaunch({
								url: "../login/login"
							})
						}
					});
				}
			},
			tip(is, text) {
				if (is == 1) {
					uni.showToast({
						icon: 'success',
						title: text
					});
				} else if (is == 2) {
					uni.showToast({
						icon: 'loading',
						title: text
					});
				} else {
					uni.showToast({
						icon: 'error',
						title: text
					});
				}
			},
			tiao() {
				uni.showToast({
					title: '暂未开放',
					icon: 'error',
					duration: 2000
				});
			},
		}
	}
</script>

<style scoped>
	.content {
		min-height: 100vh;
		background: url('https://boyaltd.oss-cn-hangzhou.aliyuncs.com/2022-08/05/XeIqBphptSm0SK1659691416220805.jpg') 100% 100%/100% 100%;
	}

	.btn_eq {
		width: 200rpx;
		height: 200rpx;
		text-align: center;
		border-radius: 50rpx;
	}

	.left_bootom {
		position: absolute;
		left: 0rpx;
		bottom: 0rpx;
		width: 30%;
		z-index: 0;
	}

	.bangfu {
		width: 85%;
		margin: 0 auto;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-around;
		padding-top: 420rpx;
	}

	.kuang {
		width: 28%;
		height: 220rpx;
		margin-top: 60rpx;
		margin-left: 10rpx;
		font-size: 28rpx;
		font-weight: bold;
		text-align: center;
		background: url('../../../../static/activity/apply/new/bj.png') 100% 100%/ 100% 100%;
	}

	.kuang1 {
		width: 28%;
		height: 220rpx;
		margin-top: 60rpx;
		margin-left: 10rpx;
		font-size: 28rpx;
		font-weight: bold;
		text-align: center;
		background: url('../../../../static/activity/apply/new/bj.png') 100% 100%/ 100% 100%;
	}

	.kuang>view,
	.kk>view {
		margin-top: 25rpx;
		margin-left: -10rpx;
	}

	.kuang>image,
	.kk>image {
		width: 120rpx;
		height: 120rpx;
		margin-top: 20rpx;
	}

	.gsgg {
		margin: 80rpx auto;
		width: 280rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		background: url('../../../../static/activity/apply/new/bj.png') 100% 100%/ 100% 100%;
	}

	.cang {
		width: 200rpx;
		height: 100rpx;
		background-color: wheat;
		position: absolute;
		top: 0;
		left: 0;
	}
</style>